﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xCommons
{
    public class WaterMark
    {
        public static string PDFWatermark(string inputfilepath, string outputfilepath, string word)//, float top, float left)string ModelPicName
        {
            iTextSharp.text.pdf.PdfReader pdfReader = null;
            iTextSharp.text.pdf.PdfStamper pdfStamper = null;
            try
            {
                pdfReader = new iTextSharp.text.pdf.PdfReader(inputfilepath);
                int numberOfPages = pdfReader.NumberOfPages;
                iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
                float width = psize.Width;
                float height = psize.Height;
                pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
                //iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName);
                iTextSharp.text.pdf.PdfGState gs = new iTextSharp.text.pdf.PdfGState();
                //image.RotationDegrees = 45;
                float left = width / 2;
                float top = height / 2 - 50;
                //image.SetAbsolutePosition(left, top);
                //image.ScaleAbsolute(360, 100);
                for (int i = 1; i <= numberOfPages; i++)
                {
                    iTextSharp.text.pdf.PdfContentByte waterMarkContent = pdfStamper.GetOverContent(i);
                    gs.FillOpacity = 0.2f;
                    waterMarkContent.SetGState(gs);
                    //waterMarkContent.AddImage(image, true);
                    // 开始
                    waterMarkContent.BeginText();
                    // 设置颜色 默认为蓝色
                    waterMarkContent.SetColorFill(iTextSharp.text.Color.BLACK);
                    // content.setColorFill(Color.GRAY);
                    // 设置字体及字号
                    iTextSharp.text.pdf.BaseFont font = iTextSharp.text.pdf.BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED);
                    waterMarkContent.SetFontAndSize(font, 150);
                    // 设置起始位置
                    // content.setTextMatrix(400, 880);
                    waterMarkContent.SetTextMatrix(left, top);
                    // 开始写入水印
                    waterMarkContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, word, left,
                     top, 45);
                    waterMarkContent.EndText();
                }
                return outputfilepath;
            }
            catch (Exception ex)
            {
                return ex.Message.Trim();

            }
            finally
            {
                if (pdfStamper != null)
                    pdfStamper.Close();

                if (pdfReader != null)
                    pdfReader.Close();
            }
        }
    }
}
